package cn.swufe.edu.stu.hw_tools;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;

import cn.swufe.edu.stu.hw_tools.Cal.BmiActivity;
import cn.swufe.edu.stu.hw_tools.Cal.CalCoderActivity;
import cn.swufe.edu.stu.hw_tools.Cal.CalculatorActivity;
import cn.swufe.edu.stu.hw_tools.Cal.DepositActivity;
import cn.swufe.edu.stu.hw_tools.Convert.Are_CV_Activity;
import cn.swufe.edu.stu.hw_tools.Convert.Dis_CV_Activity;
import cn.swufe.edu.stu.hw_tools.Convert.Hei_CV_Activity;
import cn.swufe.edu.stu.hw_tools.Convert.Hui_CV_Activity;
import cn.swufe.edu.stu.hw_tools.Time.DailyActivity;
import cn.swufe.edu.stu.hw_tools.Time.SetTimeActivity;
import cn.swufe.edu.stu.hw_tools.Time.TimerActivity;
import cn.swufe.edu.stu.hw_tools.Time.TomatoTimeActivity;

public class MainList_P1 extends AppCompatActivity {
    private long lastClickTime = 0;
    private int clickCount = 0;
    private MediaPlayer mediaPlayer;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_list_p1);
    }


    ActivityResultLauncher luncher =registerForActivityResult(
            new ActivityResultContracts.StartActivityForResult(),
            new ActivityResultCallback<ActivityResult>(){

                @Override
                public void onActivityResult(ActivityResult result) {

                }
            });

    //按钮事件
    public void onClick(View btn){
        playClickSound();
        if(getResources().getResourceEntryName(btn.getId()).equals("DW1")){
            Intent config = new Intent(this, Dis_CV_Activity.class);
            luncher.launch(config);
        }else if(getResources().getResourceEntryName(btn.getId()).equals("DW2")){
            Intent config = new Intent(this, Are_CV_Activity.class);
            luncher.launch(config);
        }else if(getResources().getResourceEntryName(btn.getId()).equals("DW3")){
            Intent config = new Intent(this, Hei_CV_Activity.class);
            luncher.launch(config);
        }else if(getResources().getResourceEntryName(btn.getId()).equals("DW4")){
            Intent config = new Intent(this, Hui_CV_Activity.class);
            luncher.launch(config);
        }else if(getResources().getResourceEntryName(btn.getId()).equals("CC1")){
            Intent config = new Intent(this, CalculatorActivity.class);
            luncher.launch(config);
        }else if(getResources().getResourceEntryName(btn.getId()).equals("CC2")){
            Intent config = new Intent(this, CalCoderActivity.class);
            luncher.launch(config);
        }else if(getResources().getResourceEntryName(btn.getId()).equals("CC3")){
            Intent config = new Intent(this, BmiActivity.class);
            luncher.launch(config);
        }else if(getResources().getResourceEntryName(btn.getId()).equals("CC4")){
            Intent config = new Intent(this, DepositActivity.class);
            luncher.launch(config);
        }else if(getResources().getResourceEntryName(btn.getId()).equals("XX1")){
            Intent config = new Intent(this, TomatoTimeActivity.class);
            luncher.launch(config);
        }else if(getResources().getResourceEntryName(btn.getId()).equals("XX2")){
            Intent config = new Intent(this, TimerActivity.class);
            luncher.launch(config);
        }else if(getResources().getResourceEntryName(btn.getId()).equals("XX3")){
            Intent config = new Intent(this, SetTimeActivity.class);
            luncher.launch(config);
        }else if(getResources().getResourceEntryName(btn.getId()).equals("XX4")){
            Intent config = new Intent(this, DailyActivity.class);
            luncher.launch(config);
        }else{
            Toast.makeText(this, "不是点我哦(๑¯ω¯๑)，是下面的按钮~", Toast.LENGTH_SHORT).show();
        }
    }

    public void CD(View btn){
        long currentTime = System.currentTimeMillis();
        if (currentTime - lastClickTime < 500) {  // 允许0.5秒内的连续点击
            clickCount++;
            if (clickCount >= 3) {
                Toast.makeText(MainList_P1.this, "(灬╹ω╹灬)", Toast.LENGTH_SHORT).show();
                clickCount = 0;
            }
            lastClickTime = currentTime;
        } else {
            clickCount = 1;  // 重置点击次数，因为间隔超过了1秒
            lastClickTime = currentTime;
        }
    }

    private void playClickSound() {
        // 确保MediaPlayer实例在播放前被释放
        if (mediaPlayer != null) {
            mediaPlayer.release();
        }

        // 创建MediaPlayer实例并播放音效
        mediaPlayer = MediaPlayer.create(this, R.raw.a_sound2);
        if (mediaPlayer != null) {
            mediaPlayer.start();
            mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    // 播放完成后释放MediaPlayer资源
                    mp.release();
                }
            });
        }
    }

}